#include "aerial\Object\DisplayBoard.h"
#include "aerial\Scene\SceneManager.h"
#include "aerial\Core\NetIO.h"

using namespace air;

DisplayBoard::DisplayBoard(hgeResourceManager* _hgeResMan, float w, float h, Event* _evnt)
	: IGuiObject(_hgeResMan), evnt(_evnt), status(false)
{
	infoId = -1;
	frame_norm = hgeResMan->GetSprite("spr.framenorm");
	frame_over = hgeResMan->GetSprite("spr.frameover");
	tick = hgeResMan->GetSprite("spr.tick");
	frame = frame_norm;
	rect.Set(0, 0, w, h);
	fnt = hgeResMan->GetFont("fnt.original");
	chosen = false;
}

bool DisplayBoard::addToGui(SceneManager* sceneMan, IGuiObject* _anchor)
{
	sceneMan->addGuiObject(this);
	sceneMan->bindGuiObjectEvent(id, evnt);
	anchor = _anchor;
	return true;
}

void DisplayBoard::Render()
{
	calcAbsolutePosition();
	if (status) {
		if (netIO->getMeId() == infoId) frame->SetColor(0xa0ff0000);
		else frame->SetColor(0xffffffff);
		frame->Render(rect.x1, rect.y1);
		PlayerDescriptor* pd = netIO->GetPlayerInfo();

		const char* usrName = pd[infoId].userName;
		int type = pd[infoId].flyObjectType;

		fnt->SetColor(0xff000000);
		fnt->printf(rect.x1 + 20, rect.y1, HGETEXT_LEFT, usrName);
		if (type == 1)	fnt->printf(rect.x1 + 400, rect.y1, HGETEXT_LEFT, "Scout");
		else if (type == 2) fnt->printf(rect.x1 + 400, rect.y1, HGETEXT_LEFT, "Fighter");
		else if (type == 3) fnt->printf(rect.x1 + 400, rect.y1, HGETEXT_LEFT, "Commander");
		else if (type == 0) fnt->printf(rect.x1 + 400, rect.y1, HGETEXT_LEFT, "");
		else fnt->printf(rect.x1 + 400, rect.y1, HGETEXT_LEFT, "Unknown");

		if ( pd[infoId].ready )
		{
			tick->Render( rect.x1 + ( rect.x2 - rect.x1 ) * 0.9f, rect.y1 );
		}
	}
	else {
		frame->SetColor(0xffffffff);
		frame->Render(rect.x1, rect.y1);
	}
}

void DisplayBoard::EnableBoard(int id)
{
	status = true;
	infoId = id;
}

void DisplayBoard::DisableBoard()
{
	status = false;
}

